package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.device.relay.RelayMessageHandler;
import com.csun.cmny.provider.device.relay.entity.RelayCmdStachEntity;
import com.csun.cmny.provider.model.domain.DmcRfidReaderGroupAlarmLight;
import com.csun.cmny.provider.model.dto.alarmlight.AlarmLightDto;
import com.csun.cmny.provider.model.dto.alarmlight.AlarmLightQueryDto;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.AlarmLightVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcAlarmLightLocationService;
import com.csun.cmny.provider.service.DmcRfidReaderGroupAlarmLightService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-08-22 11:41
 * @Description: the class dmc alarm light controller
 */
@RestController
@RequestMapping(value = "/alarmLight", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcAlarmLightController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcAlarmLightLocationService dmcAlarmLightLocationService;
    @Resource
    private DmcRfidReaderGroupAlarmLightService dmcRfidReaderGroupAlarmLightService;
    @Resource
    private RelayMessageHandler relayMessageHandler;

    /**
     * 添加报警灯
     *
     * @param alarmLightDto the alarm light dto
     * @param br            the bind result br
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper add(@Validated @RequestBody AlarmLightDto alarmLightDto,
                       BindingResult br) {

        Integer result = dmcAlarmLightLocationService.addAlarmLight(alarmLightDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.ok(cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 修改报警灯
     *
     * @param alarmLightDto the alarm light dto
     *
     * @return  the wrapper
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper edit(@RequestBody AlarmLightDto alarmLightDto) {

        Integer result = dmcAlarmLightLocationService.editAlarmLight(alarmLightDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);

    }

    /**
     * 根据Id修改报警灯
     *
     * @param id    the alarm light id
     *
     * @return  the wrapper
     */
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteById(@PathVariable Long id) {

        Integer result = dmcAlarmLightLocationService.deleteAlarmLightById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);

    }

    /**
     *  查询报警灯列表 - 分页查询
     *
     * @param alarmLightQueryDto    the alarm light query dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryListWithPage(@RequestBody AlarmLightQueryDto alarmLightQueryDto) {

        alarmLightQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        alarmLightQueryDto.setDeviceType(DmcDeviceTypeEnum.ALARM_LIGHT.getIndex());

        List<AlarmLightVo> alarmLightVoList = dmcAlarmLightLocationService
                .queryAlarmLightListWithPage(alarmLightQueryDto);

       // return WrapMapper.ok(new PageInfo<>(alarmLightVoList));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),new PageInfo<>(alarmLightVoList));
    }

    /**
     * 绑定报警灯和门禁组
     *
     * @param dmcRfidReaderGroupAlarmLight  the dmc rfid reader group alarm light
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    public Wrapper bind(@Validated @RequestBody DmcRfidReaderGroupAlarmLight dmcRfidReaderGroupAlarmLight,
                        BindingResult br) {

        Integer result = dmcRfidReaderGroupAlarmLightService.bind(dmcRfidReaderGroupAlarmLight);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(cmnyMessageSource.getMessage("operation.success", null),result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);

    }

    /**
     * 解绑报警灯和门禁组
     *
     * @param dmcRfidReaderGroupAlarmLight the dmc rfid reader group alarm light
     *
     * @return the wrapper
     */
    @ValidateAnnotation
    @RequestMapping(value = "/untied", method = RequestMethod.POST)
    public Wrapper untied(@Validated @RequestBody DmcRfidReaderGroupAlarmLight dmcRfidReaderGroupAlarmLight,
                          BindingResult br) {

        Integer result = dmcRfidReaderGroupAlarmLightService.untied(dmcRfidReaderGroupAlarmLight);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(cmnyMessageSource.getMessage("operation.success", null),result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 测试 -- 设置网络继电器
     *
     * @param relayCmdStachEntity
     *
     * @return
     */
    @RequestMapping(value = "/setStach", method = RequestMethod.POST)
    public Wrapper setStach(@RequestBody RelayCmdStachEntity relayCmdStachEntity) {

        relayMessageHandler.setStach(relayCmdStachEntity);

        //return WrapMapper.ok(cmnyMessageSource.getMessage("operation.success", null));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    /**
     * 测试 -- 启动网络继电器
     *
     * 先闭合，延迟1000ms后松开
     *
     * @param relayCmdStachEntity
     *
     * @return
     */
    @RequestMapping(value = "/startStach", method = RequestMethod.POST)
    public Wrapper startStach(@RequestBody RelayCmdStachEntity relayCmdStachEntity) {

        try {
            relayCmdStachEntity.setSta(1);
            relayMessageHandler.setStach(relayCmdStachEntity);

            Thread.sleep(1000);

            relayCmdStachEntity.setSta(0);
            relayMessageHandler.setStach(relayCmdStachEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //return WrapMapper.ok(cmnyMessageSource.getMessage("operation.success", null));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));

    }

}
